<template>
  <div>
    <el-upload
    class="avatar-uploader"
    action=""
    :show-file-list="false"
    :on-success="handleAvatarSuccess"
    :before-upload="beforeAvatarUpload"
  >
    <img v-if="imageUrl" :src="`http://localhost:9191${imageUrl }`" class="avatar" />
    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
  </el-upload>
  <el-button >确认修改</el-button>
  </div>
</template>

<script setup>
import { inject, ref } from 'vue';
import { Plus } from '@element-plus/icons-vue'

const $axios = inject('$axios');

// 上传成功的头像地址
let imageUrl = ref('');

// 获取当前登录者的账号
let user =JSON.parse(sessionStorage.getItem('user'))

// 获取会话存储内的token值
let Token = ref(user.token)

// 请求登录者的个人基本信息
const GetUerInfo =()=>{

    // 拼接请求地址
    let url ='/userinfo/'+user.username;

    // 请求个人信息
    $$axios.get(url).then((res)=>{
        if (res.code === '200') {
            // 先显示原来的头像
            imageUrl.value = res.data.avatarUrl
        }
        
    }).catch((eer)=>{
        console.log(eer);     
    }) 
}
// 调用请求函数
GetUerInfo();

// 创建函数：验证当前上传的文件是图片文件
const beforeAvatarUpload =(file)=>{

    return false;
}


</script>

<style>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
.avatar{
    width: 250px;
    height: 250px;
}
</style>